package com.zzu.chat.chat.detail;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.xuexiang.xui.widget.button.roundbutton.RoundButton;
import com.zzu.chat.R;

import cn.leancloud.AVObject;
import cn.leancloud.AVUser;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class Chat_Add_CommentActivity extends AppCompatActivity {
    private RoundButton bt_addComment;
    private EditText et_add_comment;
    private String text,id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat__add__comment);
        bt_addComment = findViewById(R.id.bt_chatAddComment_addComment);
        et_add_comment = findViewById(R.id.et_add_comment);
        addListener_addComment();
    }
   /**
    post comment*/
    public void add_comment(){
        AVObject todo = new AVObject("Comment");

        AVUser avUser = AVUser.getCurrentUser();
        todo.put("username", avUser.getUsername());
        todo.put("commentContent", text);
        id=getIntent().getStringExtra("id");
        todo.put("blink_objectId",id);
        todo.saveInBackground().subscribe(new Observer<AVObject>() {
            public void onSubscribe(Disposable disposable) {}
            public void onNext(AVObject todo) {
                // 成功保存之后，执行其他逻辑
                System.out.println("保存成功。objectId：" + todo.getObjectId());

            }
            public void onError(Throwable throwable) {
                // 异常处理
            }
            public void onComplete() {}
        });

    }
    /**
     * post blink
     * beside ,save objectId of blink for show it in blink list
    * */
    public void add_blink(){
        AVObject todo = new AVObject("Blink");

        todo.put("username", "张家兴");
        todo.put("content", text);
        id=getIntent().getStringExtra("id");
        todo.put("blink_objectId",id);
        todo.saveInBackground().subscribe(new Observer<AVObject>() {
            public void onSubscribe(Disposable disposable) {}
            public void onNext(AVObject todo) {
                // 成功保存之后，执行其他逻辑
                System.out.println("保存成功。objectId：" + todo.getObjectId());

            }
            public void onError(Throwable throwable) {
                // 异常处理
            }
            public void onComplete() {}
        });


    }
    /**
     * add listener for {@link Chat_Add_CommentActivity#bt_addComment}
     * attention,this method also add blink!!!!!
    * */
    public void addListener_addComment(){
        bt_addComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text = et_add_comment.getText().toString();
                try {
                    add_comment();
                    add_blink();//add blink
                }catch (Exception e){
                    Log.e("点击发布评论按钮","发布失败！！！");
                }
                /*startActivity(new Intent(Chat_Add_CommentActivity.this,Chat_CommentActivity.class));*/
                setResult(Activity.RESULT_OK,new Intent());
                finish();
            }
        });
    }
}